﻿#Region "Copyright"

' Copyright 2007 Ben Reichelt - http://benreichelt.net
' 
' Licensed under the Apache License, Version 2.0 (the "License");
' you may not use this file except in compliance with the License.
' You may obtain a copy of the License at
' 
'     http://www.apache.org/licenses/LICENSE-2.0
' 
' Unless required by applicable law or agreed to in writing, software
' distributed under the License is distributed on an "AS IS" BASIS,
' WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
' See the License for the specific language governing permissions and
' limitations under the License.


#End Region

Imports NUnit.Framework
Imports SqlPlastic
Imports System.Xml.Linq

<TestFixture()> _
Public Class XmlMetaDataTests

    Private metaData As XmlMetaData

    <SetUp()> _
    Public Sub Setup()
        metaData = ObjectMother.CreateDefaultMetaData
    End Sub

    <Test()> _
    Public Sub ManualLoadIsNotNullTest()
        Assert.IsNotNull(metaData)
    End Sub

    <Test()> _
    Public Sub FindTableTest()
        Dim table As MetaDataTable = metaData.FindTable(C.Table1Name)
        Assert.IsNotNull(table)
    End Sub

    <Test(), ExpectedException(GetType(TableNotFoundException))> _
    Public Sub TryFindNonExistantTable()
        Dim table As MetaDataTable = metaData.FindTable("INVALID-TABLE-NAME")
    End Sub

    <Test()> _
    Public Sub FindAllColumnsSingleTest()
        Dim doc As XmlMetaData = ObjectMother.CreateMetaDataDocument(1)
        Dim list As List(Of MetaDataColumn) = doc.FindAllColumns()
        Assert.AreEqual(1, list.Count)
    End Sub

    <Test()> _
    Public Sub FindAllColumnsZeroTest()
        Dim doc As XmlMetaData = ObjectMother.CreateMetaDataDocument(0)
        Dim list As List(Of MetaDataColumn) = doc.FindAllColumns()
        Assert.AreEqual(0, list.Count)
    End Sub

    <Test()> _
    Public Sub FindAllColumnsTwoTest()
        Dim doc As XmlMetaData = ObjectMother.CreateMetaDataDocument(2)
        Dim list As List(Of MetaDataColumn) = doc.FindAllColumns()
        Assert.AreEqual(2, list.Count)
    End Sub

    <Test()> _
    Public Sub FindAllAssociationsZeroTest()
        Dim doc As XmlMetaData = ObjectMother.CreateMetaDataDocument(0, 0)
        Dim list As List(Of MetaDataAssociation) = doc.FindAllAssociations()
        Assert.AreEqual(0, list.Count)
    End Sub

    <Test()> _
    Public Sub FindAllAssociationsOneTest()
        Dim doc As XmlMetaData = ObjectMother.CreateMetaDataDocument(0, 1)
        Dim list As List(Of MetaDataAssociation) = doc.FindAllAssociations()
        Assert.AreEqual(1, list.Count)
    End Sub

    <Test()> _
    Public Sub FindAllAssociationsTwoTest()
        Dim doc As XmlMetaData = ObjectMother.CreateMetaDataDocument(0, 2)
        Dim list As List(Of MetaDataAssociation) = doc.FindAllAssociations()
        Assert.AreEqual(2, list.Count)
    End Sub

    <Test()> _
    Public Sub FindSourceRefsTest()
        Dim list As List(Of MetaDataAssociation) = metaData.FindSourceAssociationReferences(C.Column2Member)
        Assert.AreEqual(1, list.Count)
    End Sub

    <Test()> _
    Public Sub FindDestRefsTest()
        Dim list As List(Of MetaDataAssociation) = metaData.FindDestinationAssociationReferences(C.Column1Member)
        Assert.AreEqual(3, list.Count)
    End Sub


    <Test()> _
    Public Sub FindAssociationsByColumnName()
        Dim list As List(Of MetaDataAssociation) = metaData.FindAllAssociationsByColumn(C.Column1Member)
        Assert.AreEqual(5, list.Count)
    End Sub

    <Test()> _
    Public Sub FindAllTypesTest()
        Dim list As List(Of MetaDataType) = metaData.FindAllTypes()
        Assert.AreEqual(4, list.Count)
    End Sub

    <Test()> _
    Public Sub FindTypeTest()
        Dim type As MetaDataType = metaData.FindType(C.Type1Name)
        Assert.IsNotNull(type)
    End Sub

    <Test(), ExpectedException(GetType(TypeNotFoundException))> _
    Public Sub TryFindTypeTest()
        Dim type As MetaDataType = metaData.FindType("INVALID-TYPE-NAME")
    End Sub

    <Test()> _
    Public Sub FindAllTablesTest()
        Dim list As List(Of MetaDataTable) = metaData.FindAllTables()
        Assert.AreEqual(4, list.Count)
    End Sub

    <Test(), ExpectedException(GetType(TableNotFoundException))> _
    Public Sub RemoveTableTest()
        metaData.RemoveTable(C.Table1Name)
        metaData.FindTable(C.Table1Name)
    End Sub

    <Test(), ExpectedException(GetType(ArgumentNullException))> _
    Public Sub RemoveTableTestNullName()
        metaData.RemoveTable(Nothing)
    End Sub

    <Test()> _
    Public Sub FindAssocByTypeTest()
        Dim list As List(Of MetaDataAssociation) = metaData.FindAllAssociationsByType(C.Type2Name)
        Assert.AreEqual(1, list.Count)
    End Sub

    <Test()> _
    Public Sub RemoveTableAndTypeRefsTest()
        metaData.RemoveTable(C.NormalTable)
        Dim list As List(Of MetaDataAssociation) = metaData.FindAllAssociationsByType(C.Type2Name)
        Assert.AreEqual(0, list.Count)
    End Sub

End Class
